package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.pojo.RecommendUser;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.server.vo.TodayBest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class TodayBestService {
    @DubboReference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;
    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUserId;
    @Autowired
    private UserService userService;

    //查询今日佳人
    public Object queryTodayBest(String token) {
        Long userId = this.userService.checkToken(token);
        if (null==userId){
            return ErrorResult.builder().errCode("401").errMessage("账号异常,请重新登录!").build();
        }
        RecommendUser recommendUser = this.recommendUserApi.queryWithMaxScore(userId);
        //没有每日佳人数据返回一个默认用户
        if (ObjectUtil.isEmpty(recommendUser)){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(defaultUserId);
            recommendUser.setToUserId(userId);
            recommendUser.setScore(RandomUtil.randomDouble(80,100));
        }
        UserInfo userInfo = this.userInfoApi.queryByUserId(recommendUser.getUserId());
        TodayBest todayBest = new TodayBest();
        todayBest.setFateValue(Convert.toLong(recommendUser.getScore()));//缘分值
        //hutool的api
        BeanUtil.copyProperties(userInfo,todayBest,"id");
        todayBest.setId(userInfo.getUserId());
        todayBest.setGender(userInfo.getSex().name().toLowerCase());
        return todayBest;
    }
}
